import Controller from './Controller';
import BooleanController from './BooleanController';

export default class BooleanMoreController extends Controller {

	constructor( parent, object, property,subKeys,xxconfig) {

		super( parent, object, property, "boolean", 'label',xxconfig);

		this.$span = document.createElement("span") ;
		this.$span.classList.add("controller") ;
		this.$span.classList.add("booleanMoreDataRow") ;

		this.$obj = object ;
		if(property != null && property != "")
			this.$obj = object[property] ;

		let config = {autoAddDom:false,className:"booleanMore",checkboxClass:"booleanMoreCheck",addParentControllerStyle:false} ;
		let keyArray = subKeys.split(",") ;
		this.$array = [] ;
		for(let i=0; i<keyArray.length; i++){
			let key = keyArray[i] ;
			if(this.$obj[key+"_enable"] === false)
				continue ;
			let tooltip = this.$obj[key+"_tooltip"] ;
			let endHtml = this.$obj[key+"_endHtml"] ;
			if(endHtml != null && endHtml != "")
				config.endHtml = endHtml ;
			let negate = this.$obj[key+"_negate"] ;
			if(negate === true)
				config.negate = true ;
			else
				config.negate = false ;
			let booleanController = new BooleanController(parent,this.$obj,key,config);
			if(tooltip != null && tooltip != "")
				booleanController.tooltip(tooltip) ;
			booleanController.$eventController = this ;
			this.$span.appendChild(booleanController.domElement) ;
			this.$array.push(booleanController) ;
		}

		this.$widget.appendChild( this.$span );
		this.updateDisplay();
	}

	updateDisplay() {
		for(let i=0; i<this.$array.length; i++){
			this.$array[i].updateDisplay() ;
		}
		return this;
	}

	onChange( callback ) {
		let that = this ;
		this._onChange = callback;

		for(let i=0; i<this.$array.length; i++){
			this.$array[i]._onChange = function(value){
				callback.call( this, that.$obj,this,this.property,value);
			} ;
		}
		return this;
	}

	onFinishChangeFirst( callback ) {
		let that = this ;
		this._onFinishChangeFirst = callback;
		for(let i=0; i<this.$array.length; i++){
			let _controller = this.$array[i] ;
			_controller._onFinishChangeFirst = function(value){
				callback.call( _controller, that.$obj,that,_controller,_controller.property,value);
			} ;
		}

		return this;
	}

	onFinishChange( callback ) {
		let that = this ;
		this._onFinishChange = callback;
		for(let i=0; i<this.$array.length; i++){
			let _controller = this.$array[i] ;
			_controller._onFinishChange = function(value){
				callback.call( _controller, that.$obj,that,_controller,_controller.property,value);
			} ;
		}

		return this;
	}

	/**
	 * 获取下级组件
	 * @param key
	 * @returns {null|*}
	 */
	getSubController(key){
		if(key == null || key == "") return null ;
		for(let i=0; i<this.$array.length; i++) {
			let _controller = this.$array[i];
			if(_controller.property == key)
				return _controller ;
		}
		return null ;
	}
}
